package hibernate.web;

import hibernate.fileControler.CheckFileHash;
import hibernate.fileControler.MyException;
import hibernate.model.Aygit;
import hibernate.model.Bellekler;
import hibernate.model.Resimler;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("BelleklerBean")
@Scope("session")
public class BelleklerBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;

	private Bellekler selectedBellek;
	private List<Bellekler> bellekList;
	private List<Resimler> selectedBlkResimList;
	private Resimler selectedBlkResim;

	private SirketprofilBean firmaBean;
	private ResimlerBean resimBean;
	private ArrayList<UploadedFile> fileList = new ArrayList<UploadedFile>();

	@Override
	@PostConstruct
	public void init() {
	}

	@Override
	public String restart() {
		init();
		return "list";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedBellek);
		init();
		return "list";
	}

	@Override
	public String viewNew() {
		System.out.println("Bellek viewNew");
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.setYeniUrun(true);
		Sirketprofil srf = ((SirketprofilBean) emmyService
				.getBean(new SirketprofilBean())).getCurrentSirketpro();
		Aygit agt = new Aygit(srf, "BELLEK", "", "");

		selectedBellek = new Bellekler();
		selectedBellek.setAygit(agt);
		selectedBellek.setSirketprofil(srf);
		agtBean.setSelectedAygitObject(selectedBellek);
		agtBean.setEdit(true);
		return "";
	}

	@Override
	public String viewDetail() { 
		/*
		List<Bellekler> valueList = (List<Bellekler>) belleklerTable.getValue();
		System.out.println("table size : " + valueList.size());
		Iterator<SimpleRowKey> keys = belleklerTable.getSelection().getKeys();

		if (keys.hasNext()) {
			currentBellek = valueList.get(keys.next().intValue());
			System.out.println("dolu = " + currentBellek.getAygit());
			return "detail";
		} else {
			System.out.println("boşta");
			return null;
		}
		*/
		return "detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.setSelectedAygitObject(selectedBellek);
		resimBean = (ResimlerBean) emmyService.getBean(new ResimlerBean());
		// resimBean.setDest(resimBean.getDest()+"Bellekler/");
		try {
			if (resimBean.SaveOrUpdate().equals("OK")) {
				fileList = resimBean.getFileList();
				Set<Resimler> resCollec = new HashSet<Resimler>();
				for (int i = 0; i < fileList.size(); i++) {
					Resimler res = new Resimler();
					res.setResimKonum("../productImages/Bellekler/"
							+ (CheckFileHash.fileCheck(fileList.get(i)) + fileList
									.get(i).getFileName()).toLowerCase());
					res.setAygit(selectedBellek.getAygit());
					resCollec.add(res);
					System.out.println(resCollec.size());
				}
				selectedBellek.getAygit().setResimlers(resCollec);
				emmyService.saveOrUpdate(selectedBellek);
				agtBean.deleteSelectedAygit();
				emmyService.clearSession(resimBean);
			}
		} catch (Exception e) {
			System.out.println("Bellekler Save error : " + e);
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public String getDetayliBellekler() {
		bellekList = (List<Bellekler>) emmyService.getQuery(createQuery());
		return "list";
	}

	@Override
	public String createQuery() {
		Bellekler blk = selectedBellek;
		String query = "from Bellekler";

		if (blk == null) {
			return query;
		}
		if (blk.getSirketprofil().getFirmaid() != 0) {
			if (query.indexOf("where") != -1) {
				query += " and ureticiid=" + blk.getSirketprofil().getFirmaid()
						+ "";

			} else {
				query += " where ureticiid="
						+ blk.getSirketprofil().getFirmaid() + "";
			}
		}
		if (blk.getKapasite() != null) {
			if (query.indexOf("where") != -1) {
				query += " and kapasite like '%" + blk.getKapasite() + "%'";

			} else {
				query += " where kapasite like '%" + blk.getKapasite() + "%'";
			}
		}
		if (blk.getRenk() != null) {
			if (query.indexOf("where") != -1) {
				query += " and renk like '%" + blk.getRenk() + "%'";

			} else {
				query += " where renk like '%" + blk.getRenk() + "%'";
			}
		}
		if (blk.getTipi() != null) {
			if (query.indexOf("where") != -1) {
				query += " and tipi like '%" + blk.getTipi() + "%'";

			} else {
				query += " where tipi like '%" + blk.getTipi() + "%'";
			}
		}
		return query;
	}

	@Override
	public void getSession() {

	}

	public Bellekler getSelectedBellek() {
		return selectedBellek;
	}

	public void setSelectedBellek(Bellekler selectedBellek) {
		this.selectedBellek = selectedBellek;
	}

	public List<Bellekler> getBellekList() {
		return bellekList;
	}

	public void setBellekList(List<Bellekler> bellekList) {
		this.bellekList = bellekList;
	}

	public List<Resimler> getSelectedBlkResimList() {
		return selectedBlkResimList;
	}

	public void setSelectedBlkResimList(List<Resimler> selectedBlkResimList) {
		this.selectedBlkResimList = selectedBlkResimList;
	}

	public Resimler getSelectedBlkResim() {
		return selectedBlkResim;
	}

	public void setSelectedBlkResim(Resimler selectedBlkResim) {
		this.selectedBlkResim = selectedBlkResim;
	}

	public SirketprofilBean getFirmaBean() {
		return firmaBean;
	}

	public void setFirmaBean(SirketprofilBean firmaBean) {
		this.firmaBean = firmaBean;
	}

	public ArrayList<UploadedFile> getFileList() {
		return fileList;
	}

	public void setFileList(ArrayList<UploadedFile> fileList) {
		this.fileList = fileList;
	}

	public ResimlerBean getResimBean() {
		return resimBean;
	}

	public void setResimBean(ResimlerBean resimBean) {
		this.resimBean = resimBean;
	}
}